extends Control

var tween_rot : Tween
var ready_scene
var play_scene
var ready_scene_packed_scene : PackedScene
var play_scene_packed_scene : PackedScene

func _ready() -> void:
	# 预加载场景文件
	ready_scene_packed_scene = preload("res://gameScene/readyScene/ready.tscn")
	play_scene_packed_scene = preload("res://gameScene/playScene/play.tscn")
	ready_scene = ready_scene_packed_scene.instantiate()
	add_child(ready_scene)
	ready_scene.get_node("SelectRound").connect("start_game", Callable(self, "_on_start_game"))
	
func _on_start_game():
	play_scene = play_scene_packed_scene.instantiate() #展示游玩页面
	add_child(play_scene)
	play_scene.position = Vector2(play_scene.position.x + 1280,play_scene.position.y)
	play_scene.get_node("ChangeMoney").connect("success_game",Callable(self,"_on_success_game"))
	play_scene.connect("lose_game",Callable(self,"_on_lose_game"))
	showAndHideScene(play_scene,ready_scene,1280)
	
func _on_success_game():
	ready_scene = ready_scene_packed_scene.instantiate()
	add_child(ready_scene)
	ready_scene.get_node("SelectRound").connect("start_game", Callable(self, "_on_start_game"))
	ready_scene.position = Vector2(ready_scene.position.x - 1280,ready_scene.position.y)
	showAndHideScene(ready_scene,play_scene,-1280)
	await get_tree().create_timer(1).timeout  # 等待1s

func _on_lose_game():
	#将玩家detail初始化
	PlayerDetail.functionCardArr = []
	PlayerDetail.functionCardCarryArr = []
	PlayerDetail.functionPropArr = []
	PlayerDetail.playerMoney = 0
	PlayerDetail.playerLostMoney = 0
	PlayerDetail.mahjongDeckObject = Deck.new()
	PlayerDetail.playerMahjongDeck = PlayerDetail.mahjongDeckObject.getDeck()
	RoundDetail.currentBigRound = 1
	RoundDetail.currentSmallRound = 1
	#商店初始化
	ShopDetail.functionCardArr = ShopDetail.originalFunctionCardArr
	#道具初始化
	#PropDetail.functionPropDeck = PropDetail.copyDeck
	
	ready_scene = ready_scene_packed_scene.instantiate()
	add_child(ready_scene)
	ready_scene.get_node("SelectRound").connect("start_game", Callable(self, "_on_start_game"))
	ready_scene.position = Vector2(ready_scene.position.x - 1280,ready_scene.position.y)
	showAndHideScene(ready_scene,play_scene,-1280)
	await get_tree().create_timer(1).timeout  # 等待1s

func showAndHideScene(showScene,hideScene,xint):
	tween_rot = create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_BACK).set_parallel(true)
	tween_rot.tween_property(hideScene,"position",Vector2(hideScene.position.x - xint,hideScene.position.y),1)
	tween_rot.tween_property(showScene,"position",Vector2(showScene.position.x - xint,showScene.position.y),1)
	await get_tree().create_timer(1).timeout  # 等待1s
	hideScene.free()
